package com.week.tiny_video.controller;

import com.week.tiny_video.pojo.Administrator;
import com.week.tiny_video.pojo.User;
import com.week.tiny_video.pojo.sessionId;
import com.week.tiny_video.result.Result;
import com.week.tiny_video.service.AdministratorService;
import com.week.tiny_video.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;

@CrossOrigin(allowCredentials = "true")
@RestController
public class LoginController {

    @Autowired
    private UserService userService;

    @Autowired
    private AdministratorService administratorService;


    @PostMapping("user/login")
    public Result loginU(@RequestBody User user, HttpSession session){
        System.out.println("******************");
        String username = user.getUsername();
        String password = user.getPassword();
        System.out.println(username);
        user = userService.get(username, password);
        if (null == user) {
            String message = "账号密码错误";
            System.out.println("test");
            return new Result(400,message,false);
        } else {
            session.setAttribute("user",user);
            String SessionId = session.getId();
            sessionId instance = sessionId.getInstance();
            instance.setId(SessionId);
            return new Result(200);
        }
    }


    @PostMapping("admin/login")
    public Result loginA(@RequestBody Administrator administrator, HttpServletRequest request){
        System.out.println("******************");
        String username = administrator.getUsername();
        String password = administrator.getPassword();
        System.out.println(username);
        administrator = administratorService.get(username, password);
        if (null == administrator) {
            String message = "账号密码错误";
            System.out.println("test");
            return new Result(400,message,false);
        } else {
            HttpSession session = request.getSession();
            session.setAttribute("administrator",administrator);
            Administrator administrator1 = (Administrator) session.getAttribute("administrator");
            System.out.println("*************");
            System.out.println(administrator1.getNickname());
            return new Result(200);
        }
    }

    @GetMapping("user/info")
    public Result token(HttpSession session){
        User user = (User) session.getAttribute("user");
        System.out.println(session.getId());
        if(null == user){
            System.out.println("111111111111");
        }

        if(null != user){
            HashMap<String,Object> map = new HashMap<>();
            map.put("user",user);
            return new Result(200,"用户信息",true,map);
        }
        return new Result(400,"错误",false);

    }

    @GetMapping("administrator/info")
    public Result tokenA(HttpServletRequest request){
        HttpSession session = request.getSession();
        Administrator administrator = (Administrator) session.getAttribute("administrator");
        System.out.println(administrator);
        if(null != administrator){
            System.out.println("*************");
            System.out.println(administrator.getNickname());
            HashMap<String,Object> map = new HashMap<>();
            map.put("administrator",administrator);
            return new Result(200,"用户信息",true,map);
        }
        return new Result(400,"错误",false);

    }

}
